<template>
  <d2-container>
    <div style="display: flex;justify-content: space-between;">
      <el-input placeholder="请输入内容" style="width:450px" v-model="txt">
        <template slot="append"><d2-icon-svg  style="width:15px;height: 15px" name="search"/></template>
      </el-input>
      <el-button type="primary" @click="$router.push('/demo/components/game/category/add')">新增</el-button>
    </div>
    <el-table
      :data="categories"
      style="width: 100%">
      <el-table-column
        label="ID"
        width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.categoryId }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="名称"
        width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.categoryName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :page-size="page.size"
      :pager-count="8"
      layout="prev, pager, next"
      :total="page.total"
      @current-change="currentChange"
    >
    </el-pagination>
  </d2-container>

</template>

<script>
import { doDelete, doGet } from '@/api'

export default {
  name: 'game-category',
  data () {
    return {
      page: { size: 10, current: 1, total: 10 },
      categories: [],
      tableData: [{
        date: '2016-05-02',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-04',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1517 弄'
      }, {
        date: '2016-05-01',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1519 弄'
      }, {
        date: '2016-05-03',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1516 弄'
      }]
    }
  },
  methods: {
    handleEdit (index, row) {
      this.$router.push({ path: `/demo/components/game/category/upd/${row.categoryId}` })
    },
    handleDelete (index, row) {
      console.log(index, row)
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        doDelete('gameCategory', [row.categoryId]).then(res => {
          if (res) {
            this.$message.success('删除成功！')
          } else {
            this.$message.error('删除成功！')
          }
          this.getData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    currentChange (c) {
      this.page.current = c
      this.getData()
    },
    getData () {
      doGet('gameCategory', this.page).then(res => {
        this.categories = res.records
        this.page.total = res.total
      })
    }
  },
  created () {
    this.getData()
  }
}
</script>

<style scoped>

</style>
